import fs from 'fs'
import path from 'path'
import {createLogger} from '@/utils/logger'

const logger = createLogger('Downloader')

export interface DownloadOptions {
  localPath: string
  fileName: string
}

export class Downloader {
  imgFormat = 'png'
  setImgFormat(format: string) {
    this.imgFormat = format
  }
  /**
   * 下载远程图片到本地
   * @param url 远程图片URL
   * @param options 下载选项
   * @returns 返回本地相对路径
   */
  async downloadImage(url: string, options: DownloadOptions): Promise<string> {
    try {
      // 确保目标目录存在
      if (!fs.existsSync(options.localPath)) {
        fs.mkdirSync(options.localPath, {recursive: true})
      }
      const localfileName = `${options.fileName}.${this.imgFormat}`
      // 构建本地文件路径
      const localFilePath = path.join(options.localPath, localfileName)

      // 下载图片
      const response = await fetch(url)
      if (!response.ok) {
        throw new Error(`下载失败: ${response.status} ${response.statusText}`)
      }

      // 将响应内容转换为Buffer并写入文件
      const buffer = await response.arrayBuffer()
      fs.writeFileSync(localFilePath, new Uint8Array(buffer))

      // 返回相对路径
      return path.join(path.basename(options.localPath), localfileName).replace(/\\/g, '/')
    } catch (error) {
      logger.error('图片下载错误:', error)
      throw error
    }
  }

  /**
   * 从HTML/JSX内容中提取并下载图片
   * @param content 包含图片URL的内容
   * @param localPath 本地存储路径
   * @returns 替换后的内容
   */
  async processContent(content: string, localPath: string): Promise<string> {
    localPath = path.join(localPath, 'images')
    try {
      // 匹配Figma图片URL的正则表达式
      const imgRegex = /https:\/\/figma-alpha-api\.s3\.us-west-2\.amazonaws\.com\/images\/[a-f0-9-]+/g
      const matches = content.match(imgRegex)
  
      if (!matches) {
        return content
      }
  
      // 去重URL
      const uniqueUrls = [...new Set(matches)];
      
      // 创建下载任务映射
      const downloadTasks = new Map();
      
      // 并行下载所有图片
      await Promise.all(
        uniqueUrls.map(async (remoteUrl) => {
          const fileName = path.basename(remoteUrl);
          const localUrl = await this.downloadImage(remoteUrl, {
            localPath,
            fileName,
          });
          downloadTasks.set(remoteUrl, localUrl);
        })
      );
      
      // 一次性替换所有URL
      let processedContent = content;
      for (const [remoteUrl, localUrl] of downloadTasks.entries()) {
        // 使用全局替换以处理重复的URL
        const regex = new RegExp(remoteUrl.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g');
        processedContent = processedContent.replace(regex, localUrl);
      }
  
      return processedContent;
    } catch (error) {
      logger.error('内容处理错误:', error)
      throw error
    }
  }
}

export default new Downloader()
